package com.lanou.sinasports.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.sinasports.R;

import java.util.List;

/**
 * Created by dllo on 16/1/22.
 */
public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> data;
    private int hidePosition = AdapterView.INVALID_POSITION;

    public GridViewAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size()+2;
    }

    @Override
    public String getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridViewHolder gridViewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_gv_drop,parent,false);
            gridViewHolder=new GridViewHolder(convertView);
            convertView.setTag(gridViewHolder);
        }else {
            gridViewHolder= (GridViewHolder) convertView.getTag();
        }
        if(position==0){
            gridViewHolder.textView.setText("热门");

        }else if(position==1){
            gridViewHolder.textView.setText("推荐");
        }else {
            gridViewHolder.textView.setText(data.get(position-2));
        }


        return convertView;
    }

    class GridViewHolder {
        TextView textView;
        public GridViewHolder(View view) {
            textView= (TextView) view.findViewById(R.id.tv_item_drop);
        }
    }
    public void hideView(int pos) {
        hidePosition = pos;
        notifyDataSetChanged();
    }

    public void showHideView() {
        hidePosition = AdapterView.INVALID_POSITION;
        notifyDataSetChanged();
    }

    public void removeView(int pos) {
        data.remove(pos);
        notifyDataSetChanged();
    }

    //更新拖动时的gridView
    public void swapView(int draggedPos, int destPos) {
        //从前向后拖动，其他item依次前移
        if(draggedPos < destPos) {
            data.add(destPos-1, getItem(draggedPos-2));
            data.remove(draggedPos-2);
        }
        //从后向前拖动，其他item依次后移+
        else if(draggedPos > destPos) {
            data.add(destPos-2, getItem(draggedPos-2));
            data.remove(draggedPos-1);
        }
        hidePosition = destPos;
        notifyDataSetChanged();
    }
}
